package view;

import game.Game;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JLabel;

import obj_file.Question;
import data.Question_File;

import java.awt.Font;
import java.awt.Color;

import javax.swing.JButton;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.JSpinner;

public class New_Game extends JFrame{
	
	private final JLabel lbl_Question = new JLabel("");
	JLabel lbl_Num_Question = new JLabel("Cau Hoi ");
	JLabel lbl_A = new JLabel("A.");
	JLabel lbl_B = new JLabel("B.");
	JLabel lbl_C = new JLabel("C.");
	JLabel lbl_D = new JLabel("D.");
	JLabel lbl_1000000 = new JLabel("15.    1,000,000");
	JLabel lbl_500000 = new JLabel("14.    500,000");
	JLabel lbl_250000 = new JLabel("13.    250,000");
	JLabel lbl_125000 = new JLabel("12.    125,000");
	JLabel lbl_64000 = new JLabel("11.    64,000");
	JLabel lbl_32000 = new JLabel("10.    32,000");
	JLabel lbl_16000 = new JLabel("  9.    16,000");
	JLabel lbl_8000 = new JLabel("  8.    8000");
	JLabel lbl_4000 = new JLabel("  7.    4000");
	JLabel lbl_2000 = new JLabel("  6.    2000");
	JLabel lbl_1000 = new JLabel("  5.    1000");
	JLabel lbl_500 = new JLabel("  4.    500");
	JLabel lbl_300 = new JLabel("  3.    300");
	JLabel lbl_200 = new JLabel("  2.    200");
	JLabel lbl_100 = new JLabel("  1.    100");
		
	int num =0;	
	boolean result_answer = true;
	Game game;
	
	public New_Game() 
	{
		
		// Frame				
				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				setBounds(400, 20, 680, 680);
				this.setResizable(false);
				JPanel contentPane = new JPanel();
				contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
				contentPane.setLayout(null);
				setContentPane(contentPane);
				
				JLabel load_label = new JLabel("LOADING...");
				load_label.setForeground(Color.GREEN);
				load_label.setFont(new Font("Times New Roman", Font.BOLD, 39));
				
				try
				{
					load_label.setBounds(190, 212, 311, 69);
					contentPane.add(load_label);
					Game game = new Game();
				}
				catch(Exception e)
				{
					e.printStackTrace();		
				}	
				load_label.setVisible(false);
				
		// Answers Panel 
				JPanel panel = new JPanel();
				panel.setBounds(0, 442, 674, 209);
				panel.setBackground(Color.PINK);
				panel.setLayout(null);
				contentPane.add(panel);
		
		// Label Cau Hoi So
				lbl_Num_Question.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_Num_Question.setBounds(10, 11, 200, 14);
				panel.add(lbl_Num_Question);
				lbl_Question.setBackground(Color.GREEN);
				lbl_Question.setFont(new Font("Times New Roman", Font.PLAIN, 14));
				lbl_Question.setBounds(10, 30, 648, 69);
				panel.add(lbl_Question);							
				
		// Label Dap An A		
				lbl_A.setFont(new Font("Times New Roman", Font.BOLD, 14));
				lbl_A.setOpaque(true);
				lbl_A.setBackground(Color.GREEN);
				lbl_A.setBounds(10, 105, 305, 46);
				lbl_A.setBorder((new SoftBevelBorder(SoftBevelBorder.LOWERED)));
				lbl_A.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						game.Choose_Question_Color(lbl_A, lbl_B, lbl_C, lbl_D);
						result_answer = game.Test_Answer(1, lbl_Num_Question, lbl_Question, lbl_A, lbl_B, lbl_C, lbl_D);
						Score_Label_Step();
						
					}
				});
				panel.add(lbl_A);
						
		// Label Dap An B
				lbl_B.setFont(new Font("Times New Roman", Font.BOLD, 14));
				lbl_B.setOpaque(true);
				lbl_B.setBackground(Color.GREEN);
				lbl_B.setBounds(354, 105, 305, 46);
				lbl_B.setBorder((new SoftBevelBorder(SoftBevelBorder.LOWERED)));
				lbl_B.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						game.Choose_Question_Color(lbl_B, lbl_A, lbl_C, lbl_D);
						result_answer = game.Test_Answer(2, lbl_Num_Question, lbl_Question, lbl_A, lbl_B, lbl_C, lbl_D);
						Score_Label_Step();
						
					}
				});
				panel.add(lbl_B);
				
		// Label Dap An C		
				lbl_C.setFont(new Font("Times New Roman", Font.BOLD, 14));
				lbl_C.setOpaque(true);
				lbl_C.setBackground(Color.GREEN);
				lbl_C.setBounds(10, 157, 305, 46);
				lbl_A.setBorder((new SoftBevelBorder(SoftBevelBorder.LOWERED)));
				lbl_C.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						game.Choose_Question_Color(lbl_C, lbl_B, lbl_A, lbl_D);
						result_answer = game.Test_Answer(3, lbl_Num_Question, lbl_Question, lbl_A, lbl_B, lbl_C, lbl_D);
						Score_Label_Step();
						
					}
				});
				panel.add(lbl_C);
				
		// Label Dap An D
				lbl_D.setFont(new Font("Times New Roman", Font.BOLD, 14));
				lbl_D.setOpaque(true);
				lbl_D.setBackground(Color.GREEN);
				lbl_D.setBounds(354, 157, 305, 46);
				lbl_A.setBorder((new SoftBevelBorder(SoftBevelBorder.LOWERED)));
				lbl_D.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						game.Choose_Question_Color(lbl_D, lbl_B, lbl_A, lbl_C);						
						result_answer = game.Test_Answer(4, lbl_Num_Question, lbl_Question, lbl_A, lbl_B, lbl_C, lbl_D);
						Score_Label_Step();							
					}
				});
				panel.add(lbl_D);
				
		// Panel Muc Diem Va Quyen Tro Giup
				JPanel panel_1 = new JPanel();
				panel_1.setBackground(Color.ORANGE);
				panel_1.setBounds(502, 0, 172, 444);
				panel_1.setLayout(null);
				contentPane.add(panel_1);
				
		// Button Tro Giup 1
				final JButton btnNewButton = new JButton("?");
				btnNewButton.setBounds(10, 388, 45, 45);
				panel_1.add(btnNewButton);
				btnNewButton.addMouseListener(new MouseAdapter(){
					@Override
					public void mouseClicked(MouseEvent e) {
						btnNewButton.setEnabled(false);
						game.Help_Change_Question(lbl_Num_Question, lbl_Question, lbl_A, lbl_B, lbl_C, lbl_D);						
					}
				});
		// Button Tro Giup 2
				final JButton button = new JButton("II");
				button.setBounds(65, 388, 45, 45);
				button.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e)
					{
						button.setEnabled(false);
						game.Help_Choose_Again(true);											
					}
				});
				panel_1.add(button);
				
		// Button Tro Giup 3
				final JButton button_1 = new JButton("");
				button_1.setBounds(117, 388, 45, 45);
				button_1.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e)
					{
						button_1.setEnabled(false);
						game.Help_3();								
					}
				});
				panel_1.add(button_1);				
				
		// Label 1,000,000						
				lbl_1000000.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_1000000.setBounds(10, 11, 152, 22);
				panel_1.add(lbl_1000000);
				
		// Label 500,000				
				lbl_500000.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_500000.setBounds(10, 36, 152, 22);
				panel_1.add(lbl_500000);
				
		// Label 250,000				
				lbl_250000.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_250000.setBounds(10, 61, 152, 22);
				panel_1.add(lbl_250000);
				
		// Label 125,000				
				lbl_125000.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_125000.setBounds(10, 86, 152, 22);
				panel_1.add(lbl_125000);
				
		// Label 64,000				
				lbl_64000.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_64000.setBounds(10, 111, 152, 22);
				panel_1.add(lbl_64000);
				
		// Label 32,000				
				lbl_32000.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_32000.setBounds(10, 136, 152, 22);
				panel_1.add(lbl_32000);
				
		// Label 16,000
				lbl_16000.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_16000.setBounds(9, 161, 153, 22);
				panel_1.add(lbl_16000);
				
		// Label 8,000				
				lbl_8000.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_8000.setBounds(10, 186, 152, 14);
				panel_1.add(lbl_8000);
								
				lbl_4000.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_4000.setBounds(9, 211, 153, 14);
				panel_1.add(lbl_4000);
								
				lbl_2000.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_2000.setBounds(10, 236, 152, 14);
				panel_1.add(lbl_2000);
				
				lbl_1000.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_1000.setBounds(10, 261, 152, 14);
				panel_1.add(lbl_1000);
				
				lbl_500.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_500.setBounds(10, 286, 152, 14);
				panel_1.add(lbl_500);
								
				lbl_300.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_300.setBounds(10, 311, 152, 14);
				panel_1.add(lbl_300);
								
				lbl_200.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_200.setBounds(10, 336, 152, 14);
				panel_1.add(lbl_200);
				
				lbl_100.setOpaque(true);
				lbl_100.setBackground(Color.GREEN);
				lbl_100.setFont(new Font("Times New Roman", Font.BOLD, 18));
				lbl_100.setBounds(10, 361, 152, 14);
				panel_1.add(lbl_100);
				
				JPanel panel_2 = new JPanel();
				panel_2.setBounds(0, 0, 501, 444);
				panel_2.setLayout(null);
				contentPane.add(panel_2);
				
				JLabel lblNewLabel = new JLabel("New label");
				lblNewLabel.setIcon(new ImageIcon("C:\\Users\\The Flash\\Desktop\\The-best-top-desktop-red-wallpapers-red-wallpaper-red-background-hd-31.jpg"));
				lblNewLabel.setBounds(0, 0, 674, 653);
				lblNewLabel.setOpaque(true);
				panel_2.add(lblNewLabel);
			
				try {
					game = new Game();
					game.Question_Output(num,lbl_Num_Question,lbl_Question,lbl_A , lbl_B,lbl_C,lbl_D);
				} catch (ParserConfigurationException | SAXException
						| IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
	}
	
	private void Change_Color_Score(JLabel lbl_score , boolean result)
	{
		if(result == true)
		{
			lbl_score.setOpaque(true);
			lbl_score.setBackground(Color.GREEN);
		}
		else 
		{
			lbl_score.setOpaque(true);
			lbl_score.setBackground(Color.RED);
		}
	}
	
	private void Score_Label_Step()
	{
		if(result_answer == true)
		{
			switch(game.real_score)
			{
			case 100:
				Change_Color_Score(lbl_100, true);
				break;
			case 200:
				Change_Color_Score(lbl_200, true);
				break;
			case 300:
				Change_Color_Score(lbl_300, true);
				break;
			case 500:
				Change_Color_Score(lbl_500, true);
				break;
			case 1000:
				Change_Color_Score(lbl_1000, true);
				break;
			case 2000:
				Change_Color_Score(lbl_2000, true);
				break;
			case 4000:
				Change_Color_Score(lbl_4000, true);
				break;
			case 8000:
				Change_Color_Score(lbl_8000, true);
				break;
			case 16000:
				Change_Color_Score(lbl_16000, true);
				break;
			case 32000:
				Change_Color_Score(lbl_32000, true);
				break;
			case 64000:
				Change_Color_Score(lbl_64000, true);
				break;
			case 125000:
				Change_Color_Score(lbl_125000, true);
				break;
			case 250000:
				Change_Color_Score(lbl_250000, true);
				break;
			case 500000:
				Change_Color_Score(lbl_500000, true);
				break;
			case 1000000:
				Change_Color_Score(lbl_1000000, true);
				break;
			
			}
		}
		else
		{
			switch(game.real_score)
			{
			case 100:
				Change_Color_Score(lbl_100, false);
				break;
			case 200:
				Change_Color_Score(lbl_200, false);
				break;
			case 300:
				Change_Color_Score(lbl_300, false);
				break;
			case 500:
				Change_Color_Score(lbl_500, false);
				break;
			case 1000:
				Change_Color_Score(lbl_1000, false);
				break;
			case 2000:
				Change_Color_Score(lbl_2000, false);
				break;
			case 4000:
				Change_Color_Score(lbl_4000, false);
				break;
			case 8000:
				Change_Color_Score(lbl_8000, false);
				break;
			case 16000:
				Change_Color_Score(lbl_16000, false);
				break;
			case 32000:
				Change_Color_Score(lbl_32000, false);
				break;
			case 64000:
				Change_Color_Score(lbl_64000, false);
				break;
			case 125000:
				Change_Color_Score(lbl_125000, false);
				break;
			case 250000:
				Change_Color_Score(lbl_250000, false);
				break;
			case 500000:
				Change_Color_Score(lbl_500000, false);
				break;
			case 1000000:
				Change_Color_Score(lbl_1000000, false);
				break;
			}
		}
	}
}
